package com.ruoyi.demo;

import cn.hutool.core.lang.Console;
import com.ruoyi.common.core.service.ConfigService;
import com.ruoyi.common.utils.EncryptUtils;
import org.bouncycastle.jcajce.provider.digest.MD2;
import org.bouncycastle.jcajce.provider.digest.SHA1;
import org.bouncycastle.jcajce.provider.digest.SM3;

import java.util.Map;

/**
 * <简述>
 * <详细描述>
 *
 * @author syf
 * @date 2024年09月15日 16:52
 */
public class TestEncryptTest {

    public static void main(String[] args) {
        String data = "1234Hello你好";
        String password = "syf12345abcdefgh";
        //base64(data, null);
        //aes(data, password);
        //sm4(data,password);
        //sm2(data);
        //rsa(data);
        other(data);
    }

    /**
     * <简述>其他摘要算法
     * <详细描述>
     * @author syf
     * @date 2024/9/15 21:05
     * @param data
     */
    public static void other(String data){
        Console.log("md5加密前：{},加密后：{}", data, EncryptUtils.encryptByMd5(data));
        Console.log("sha256加密前：{},加密后：{}", data, EncryptUtils.encryptBySha256(data));
        Console.log("sm3加密前：{},加密后：{}", data, EncryptUtils.encryptBySm3(data));
    }


    /**
     * <简述>rsa 非对称加密
     * <详细描述>
     * @author syf
     * @date 2024/9/15 20:56
     * @param data
     */
    public static void rsa(String data) {
        Map<String, String> stringStringMap = EncryptUtils.generateRsaKey();
        String PUBLIC_KEY = stringStringMap.get(EncryptUtils.PUBLIC_KEY);
        String PRIVATE_KEY = stringStringMap.get(EncryptUtils.PRIVATE_KEY);
        Console.log("公钥：{}", stringStringMap.get(EncryptUtils.PUBLIC_KEY));
        Console.log("私钥：{}", stringStringMap.get(EncryptUtils.PRIVATE_KEY));

        String encryptByRsa = EncryptUtils.encryptByRsa(data, PUBLIC_KEY);
        Console.log("加密前{},加密后：{}", data, encryptByRsa);
        String decryptByRsa = EncryptUtils.decryptByRsa(encryptByRsa, PRIVATE_KEY);
        Console.log("解密后：{}", decryptByRsa);
    }

    /**
     * <简述>sm2 非对称加密
     * <详细描述>
     * @author syf
     * @date 2024/9/15 20:56
     * @param data
     */
    public  static  void  sm2(String data) {
        Map<String, String> stringStringMap = EncryptUtils.generateSm2Key();
        String PUBLIC_KEY = stringStringMap.get(EncryptUtils.PUBLIC_KEY);
        String PRIVATE_KEY = stringStringMap.get(EncryptUtils.PRIVATE_KEY);
        Console.log("公钥：{},私钥：{}", stringStringMap.get(EncryptUtils.PUBLIC_KEY), stringStringMap.get(EncryptUtils.PRIVATE_KEY));

        String encryptBySm2 = EncryptUtils.encryptBySm2(data, PUBLIC_KEY);
        Console.log("加密前{},加密后：{}", data, encryptBySm2);
        String decryptBySm2 = EncryptUtils.decryptBySm2(encryptBySm2, PRIVATE_KEY);
        Console.log("解密后：{}", decryptBySm2);
    }

    /**
     * <简述>sm4 对称加密
     * <详细描述>
     * @author syf
     * @date 2024/9/15 20:57
     * @param data
     * @param password
     */
    public static void sm4(String data, String password) {
        String encryptBySm4 = EncryptUtils.encryptBySm4(data, password);
        Console.log("加密前{},加密后：{}", data, encryptBySm4);
        String decryptBySm4 = EncryptUtils.decryptBySm4(encryptBySm4, password);
        Console.log("解密后：{}", decryptBySm4);
    }

    /**
     * <简述>aes 对称加密
     * <详细描述>
     * @author syf
     * @date 2024/9/15 20:57
     * @param data
     * @param password
     */
    public  static  void  aes(String data, String password) {
        String encryptByAes = EncryptUtils.encryptByAes(data, password);
        Console.log("加密前{},加密后：{}", data, encryptByAes);
        String decryptByAes = EncryptUtils.decryptByAes(encryptByAes, password);
        Console.log("解密后：{}", decryptByAes);
    }

    /**
     * <简述>base64 加密
     * <详细描述>
     * @author syf
     * @date 2024/9/15 20:57
     * @param data
     * @param password
     */
    public  static  void  base64(String data, String password) {
        //base64 加密
        String s = EncryptUtils.encryptByBase64(data);
        Console.log("加密前{},加密后：{}", data, s);
        String decryptByBase64 = EncryptUtils.decryptByBase64(s);
        Console.log("解密后：{}", decryptByBase64);
    }




}
